package hu.kisprof.quiz.view.dialogs;

import hu.kisprof.quiz.view.Labels;
import hu.kisprof.quiz.view.QuizGUI;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.border.LineBorder;

public class QuizChallengeReceivedDialog extends JDialog implements ActionListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = -1322651010065768334L;

	private QuizGUI gui;
	
	private JTextArea message;
	private JButton okButton = new JButton(Labels.accept);
	private JButton cancelButton = new JButton(Labels.decline);
	private String username;
	private int difficulty;
	private Boolean buttonPressed = null;
	
	
	public QuizChallengeReceivedDialog(String username, String message, int difficulty, QuizGUI gui){
		super(gui.getWindow(),Labels.challange,true);
		this.gui = gui;
		this.message = new JTextArea(message);
		this.message.setEditable(false);
		this.message.setBackground(new JLabel().getBackground());
		this.message.setBorder(new LineBorder(Color.BLACK));
		this.username = username;
		this.difficulty = difficulty;
		JPanel dialogPanel = new JPanel();
		dialogPanel.setLayout(new BorderLayout());
		dialogPanel.add(createSettingsPanel(),BorderLayout.CENTER);
		dialogPanel.add(createButtonspanel(),BorderLayout.SOUTH);
		
		getContentPane().add(dialogPanel);
		setMinimumSize(new Dimension(300,250));
		pack();
		setLocationRelativeTo(gui.getWindow());
		//setVisible(true);
	}

	private Component createButtonspanel() {
		JPanel buttons = new JPanel(new FlowLayout(FlowLayout.CENTER));
		buttons.add(okButton);
		okButton.addActionListener(this);
		buttons.add(cancelButton);
		cancelButton.addActionListener(this);
		
		return buttons;
	}
	public boolean showDialog(){
		setVisible(true);
		while(buttonPressed==null){
			try {
				Thread.sleep(1);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return buttonPressed;
	}
	

	private Component createSettingsPanel() {
		JPanel tmpPanel = new JPanel(new BorderLayout());
		tmpPanel.add(new JLabel(Labels.difficulty+": "),BorderLayout.WEST);
		tmpPanel.add(new JLabel(""+difficulty),BorderLayout.CENTER);
		JPanel tmpPanel2 = new JPanel(new BorderLayout());
		tmpPanel2.add(tmpPanel,BorderLayout.EAST);
		tmpPanel = new JPanel();
		tmpPanel.add(new JLabel(Labels.username+": "),BorderLayout.WEST);
		tmpPanel.add(new JLabel(""+username),BorderLayout.CENTER);
		tmpPanel2.add(tmpPanel,BorderLayout.WEST);
		
		tmpPanel = new JPanel(new BorderLayout());
		JLabel tmpLabel = new JLabel(Labels.challange,JLabel.CENTER);
		tmpLabel.setFont(new Font(tmpLabel.getFont().getFontName(),0,24));
		tmpPanel.add(tmpLabel,BorderLayout.NORTH);
		tmpPanel.add(tmpPanel2,BorderLayout.CENTER);
		
		tmpPanel2 = new JPanel(new BorderLayout());
		tmpPanel2.add(tmpPanel,BorderLayout.NORTH);
		tmpPanel2.add(message,BorderLayout.CENTER);
		
		return tmpPanel2;
		
		
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getActionCommand().equals(Labels.decline)){
			buttonPressed=false;
			setVisible(false);
		}
		if(e.getActionCommand().equals(Labels.accept)){
			buttonPressed=true;
			gui.getControl().setInGame(username, true);
			gui.showGamePanel();
			setVisible(false);
		}
	}
}
